﻿using ClientAppraise.ViewModels.Validator;
using ClientInfrastructure.Util.Helper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ShareCode.Dto.User.Org.Admin;
using ShareCode.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClientDao.Corpus;
using ClientInfrastructure.Dto;
using ShareCode.Entity.Corpus;

namespace ClientAppraise.ViewModels.Pages.Corpus.CorpusType.Admin
{
    public partial class NewViewModel(CorpusTypeDal typeDal) : MyObservableValidator
    {
        CorpusTypeDal Dal = typeDal;

        #region  属性
        [ObservableProperty]
        private int sort;
        [ObservableProperty]
        [ServerError]
        private string? theCode;
        
        [ObservableProperty]
        private string? remarks;
        [ObservableProperty]
        [ServerError]
        private string? title;
        [ObservableProperty]
        private int state;
        [ObservableProperty]
        private string? parentTitle;


        [ObservableProperty]
        private string? pageTitle;
        #endregion

       //readonly private long treeParentId;

        public PageParameterDto? DataContext { get; private set; }

        public void InitDataContext(PageParameterDto? dataContext)
        {
            this.DataContext = dataContext;

            if (DataContext != default && DataContext.Model is CorpusTypeEntity model)
            {
                if (DataContext.Do == PageParameterDtoDo.Update)
                {
                    this.Sort = model.Sort;
                    this.TheCode = model.TheCode;
                    this.Remarks = model.Remarks;
                    this.Title = model.Title;
                    this.State = model.State;
                }
                else if (DataContext.Do == PageParameterDtoDo.AddSon)
                {
                    this.ParentTitle = model.Title;
                }
                this.PageTitle = this.DataContext?.Do switch
                {
                    PageParameterDtoDo.Add => "新建语料分类",
                    PageParameterDtoDo.Update => "更新分类：" + model?.Title,
                    PageParameterDtoDo.AddSon => $"为：{model?.Title}，添加子级",
                    PageParameterDtoDo.AddAfter => "新建语料分类",
                    PageParameterDtoDo.AddBefore => "新建语料分类",
                    _ => "新建语料分类"
                };
            }
            else
            {
                this.PageTitle = "新建语料分类";
            }

        }

        [RelayCommand]
        private async Task OnSubmitAsync()
        {
            ValidateAllProperties();

            if (this.HasErrors)
            {
                App.MySnackbar.Danger("没有完成表单填写", "没有完成表单填写");
                return;
            }

            await App.MyContentDialog.ShowRingLoadingAsync(async () =>
            {
                try
                {
                    CorpusTypeEntity entity = new ShareCode.Entity.Corpus.CorpusTypeEntity
                    {
                        Title = this.Title,
                        Remarks = this.Remarks,
                        TheCode = this.TheCode,
                        Sort = this.Sort,
                        State = this.State
                    };

                    if (DataContext != default && DataContext.Model is CorpusTypeEntity model)
                    {
                        if (DataContext.Do == PageParameterDtoDo.Update)
                        {
                            entity = model;

                            entity.Title = this.Title;
                            entity.Remarks = this.Remarks;
                            entity.TheCode = this.TheCode;
                            entity.Sort = this.Sort;
                            entity.State = this.State;
                        }
                        else if (DataContext.Do == PageParameterDtoDo.AddSon)
                        {
                            entity.TreeParentId = model.Id;
                        }
                    }

                    if (entity.Id == 0)
                    {
                        ResultDto<bool> reslut = await Dal.TreeInsertSon(entity);
                        if (reslut.Success)
                        {
                            App.MySnackbar.Success("成功添加：" + entity.Title);
                            App.MyNavigation.GoBack();
                            return;
                        }

                        //显示错误信息
                        ValidateAllPropertiesWithServer(reslut?.DetailMSg ?? []);

                        if (!string.IsNullOrEmpty(reslut?.Message))
                        {
                            App.MySnackbar.Danger("添加失败:", reslut.Message);
                        }
                    }
                    else
                    {
                        ResultDto<bool> reslut = await Dal.UpdateAsync(entity);
                        if (reslut.Success)
                        {
                            App.MySnackbar.Success("成功修改：" + entity.Title);
                            App.MyNavigation.GoBack();
                            return;
                        }

                        //显示错误信息
                        ValidateAllPropertiesWithServer(reslut?.DetailMSg ?? []);

                        if (!string.IsNullOrEmpty(reslut?.Message))
                        {
                            App.MySnackbar.Danger("修改失败:", reslut.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    App.MySnackbar.Danger(ex.Message, ex.Message);
                }
                finally
                {
                    App.MyContentDialog.CloseLoading();
                }
            });
        }

    }
}
